module station_address_registers(
    input epu_clk,
    input rst_n,

    input pdi_epu_we,
    input pdi_epu_re,
    input [15:0] pdi_epu_addr,
    input [7:0] pdi_epu_din,
    output reg [7:0] epu_pdi_dout,
    output reg epu_pdi_dout_valid,

    input ecat_epu_we,
    input ecat_epu_re,
    input [15:0] ecat_epu_addr,
    input [7:0] ecat_epu_din,
    output reg [7:0] epu_ecat_dout,
    output reg epu_ecat_dout_valid,

    output reg [15:0] configured_station_address_r,
    output reg [15:0] configured_station_alias_r,

    input crc_check_success
);
    reg [15:0] configured_station_address_r_shadow;
    /* configured station address register 0x0010:0x0011 */
    always@(posedge epu_clk or negedge rst_n) begin
        if(!rst_n) begin
            configured_station_address_r <= 16'b0;
        end
        else if(crc_check_success) begin
            configured_station_address_r <= configured_station_address_r_shadow;
        end
    end
    /* configured station address shadow register  */
    always@(posedge epu_clk or negedge rst_n) begin
        if(!rst_n) begin
            configured_station_address_r_shadow <= 16'b0;
        end
        else if(pdi_epu_addr==16'h0010 && ecat_epu_we) begin
            configured_station_address_r_shadow[7:0] <= ecat_epu_din;
        end
        else if(pdi_epu_addr==16'h0011 && ecat_epu_we) begin
            configured_station_address_r_shadow[15:8] <= ecat_epu_din;
        end
    end 
    /* configured station alias register 0x0012:0x0013 */
    always@(posedge epu_clk or negedge rst_n) begin
        if(!rst_n) begin
            configured_station_alias_r <= 16'b0;
        end
        else if(pdi_epu_addr==16'h0012 && pdi_epu_we) begin
            configured_station_alias_r[7:0] <= pdi_epu_din;
        end
        else if(pdi_epu_addr==16'h0013 && pdi_epu_we) begin
            configured_station_alias_r[15:8] <= pdi_epu_din;
        end
    end
    /* ecat read */
    always@(posedge epu_clk or negedge rst_n) begin
        if(!rst_n) begin
            epu_ecat_dout <= 8'b0;
            epu_ecat_dout_valid <= 0;
        end
        else if(ecat_epu_re) begin
            case(ecat_epu_addr)
                16'h0010: begin
                    epu_ecat_dout <= configured_station_address_r[7:0];
                    epu_ecat_dout_valid <= 1;
                end
                16'h0011: begin
                    epu_ecat_dout <= configured_station_address_r[15:8];
                    epu_ecat_dout_valid <= 1;
                end
                16'h0012:  begin
                    epu_ecat_dout <= configured_station_alias_r[7:0];
                    epu_ecat_dout_valid <= 1;
                end
                16'h0013: begin
                    epu_ecat_dout <= configured_station_alias_r[15:8];
                    epu_ecat_dout_valid <= 1;
                end
                default: begin
                    epu_ecat_dout <= 8'b0;
                    epu_ecat_dout_valid <= 0;
                end
            endcase
        end
        else begin
            epu_ecat_dout <= 8'b0;
            epu_ecat_dout_valid <= 0;
        end
    end
    /* pdi read */
    always@(posedge epu_clk or negedge rst_n) begin
        if(!rst_n) begin
            epu_pdi_dout <= 8'b0;
            epu_pdi_dout_valid <= 0;
        end
        else if(pdi_epu_re) begin
            case(pdi_epu_addr)
                16'h0010: begin
                    epu_pdi_dout <= configured_station_address_r[7:0];
                    epu_pdi_dout_valid <= 1;
                end
                16'h0011: begin
                    epu_pdi_dout <= configured_station_address_r[15:8];
                    epu_pdi_dout_valid <= 1;
                end
                16'h0012:  begin
                    epu_pdi_dout <= configured_station_alias_r[7:0];
                    epu_pdi_dout_valid <= 1;
                end
                16'h0013: begin
                    epu_pdi_dout <= configured_station_alias_r[15:8];
                    epu_pdi_dout_valid <= 1;
                end
                default: begin
                    epu_pdi_dout <= 8'b0;
                    epu_pdi_dout_valid <= 0;
                end
            endcase
        end
        else begin
            epu_pdi_dout <= 8'b0;
            epu_pdi_dout_valid <= 0;
        end
    end



endmodule